package com.weng.apistarter.crud.api;

import com.weng.apistarter.crud.entity.User;
import com.weng.apistarter.crud.service.IUserService;
import com.weng.apistarter.crud.model.vo.UserVo;
import com.weng.framework.common.model.vo.PageRequestVO;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.validator.annotation.NotEmpty;
import com.weng.framework.web.model.request.DeleteReqVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import org.apache.commons.lang3.StringUtils;
import com.weng.framework.web.base.BaseController;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import cn.hutool.core.convert.Convert;


/**
 * 用户信息 API实现
 *
 * @author weng
 * @date 2021-10-03 11:22:35
 *
 */  
@Slf4j
@RestController
@RequestMapping("/users")
@Validated
@Api(value = "用户信息api", tags = {"用户信息操作接口"})
public class UserApi extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * 功能描述:单个获取用户信息
     */
    @ApiOperation(value = "获取用户信息", notes = "")
    @GetMapping(value = "/{id}")
    public ResponseData get(@PathVariable("id") String id){

        User user = userService.getById(id);
        return success(user);
    }


    /**
     * 条件 分页查询用户信息
     */
    @ApiOperation(value = "分页获取用户信息", notes = "")
    @GetMapping(value = "")
    public ResponseData list(PageRequestVO pageRequest){
		IPage<User> iPage = userService.list(pageRequest.getPageNo(), pageRequest.getPageSize());
        return success(iPage);
    }


    /**
	 * 新增用户信息
	 */
	@ApiOperation(value = "新增用户信息", notes = "")
	@PostMapping(value = "")
	public ResponseData add(@Valid @RequestBody UserVo userReq){
		User user = Convert.convert(User.class, userReq);
		userService.add(user);
		return success(user);
	}

	/**
	 * 更新用户信息
	 */
	@ApiOperation(value = "更新用户信息", notes = "")
	@PutMapping(value = "")
	public ResponseData update(@Valid @RequestBody UserVo userReq){
		User user = Convert.convert(User.class, userReq);
		userService.update(user);
		return success();
	}


	/**
	 * 删除用户信息
	 */
	@ApiOperation(value = "ids 删除用户信息", notes = "可以删除多个，中间以,号分隔")
	@DeleteMapping(value = "")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		userService.removeByIds(Arrays.asList(ids.split(",")));
		return success(deleteReq.getIds());
	}
}
